/*----------------------------------------------------------------------
  - File name      : SysTick.c
  - Author         : Liu Chao
  - Update date    : 2020-07-14                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: 
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/

#include "SysTick.h"

/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/

static uint8_t  Fac_us = 0; // Us delay multipliper
static uint16_t Fac_ms = 0; // Ms delay multipliper

/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*------------------------------------------------------------
| @Description: SysTick init function                        |
| @param      : uint8_t SYSCLK                               |
|               Systick clock is fixed to 1 / 8 of AHB clock |
| @return     : None                                         |
------------------------------------------------------------*/

void SysTick_Init(uint8_t SYSCLK)
{
    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK_DIV8);
/*
    The count times needed to save 1US by 8-frequency division of sysclk
*/
    Fac_us = SYSCLK / 8;  
/*
    Number of systick clocks required per MS
*/    
    Fac_ms = (uint16_t) Fac_us * 1000; 
}

/*------------------------------------------------------------
| @Description: SysTick delay us function                    |
| @param      : uint32_t nus                                 |
|               nus < 798915us (2^24 / Fac_us = 21)          |
| @return     : None                                         |
------------------------------------------------------------*/

void SysTick_Delay_us(uint32_t nus)
{
    uint32_t temp;
    SysTick -> LOAD = nus * Fac_us;  //Time loading
    SysTick -> VAL = 0X00;           //Clear Count
    SysTick -> CTRL |= 0x01;         //Start Countint down 
    do
    {
        temp = SysTick -> CTRL;
    } while ((temp&0x01)&& !(temp & (1<<16))); //Waiting times arrives

    SysTick -> CTRL &= ~ 0x01; // Turn off Count
    SysTick -> VAL = 0x00;  //Clear Count
}

/*------------------------------------------------------------
| @Description: SysTick delay ms function                    |
| @param      : uint16_t nms                                 |
|               nus < (0xffffff*8*1000/SYSCLK)               |
| @return     : None                                         |
------------------------------------------------------------*/

static void SysTick_Delay_nms(uint16_t nms)
{
    uint32_t temp;

    //Time loading(SysTick->LOAD = 24bit)
    SysTick -> LOAD = (uint32_t) nms * Fac_ms;
	SysTick -> VAL = 0x00;   //Clear Count
	SysTick -> CTRL |= 0x01 ; //Start Countint down 
    do
    {
        temp = SysTick -> CTRL;
    } while((temp & 0x01) && !(temp & (1<<16))); //Waiting times arrives
    
    SysTick -> CTRL &= ~ 0x01; //Turn off Count
    SysTick -> VAL = 0x00;  //Clear Count
}


/*------------------------------------------------------------
| @Description: SysTick delay ms function                    |
| @param      : uint16_t nms                                 |
|               0 <= nus <= 65536                            |
| @return     : None                                         |
------------------------------------------------------------*/

void SysTick_Delay_ms(uint16_t nms)
{
    uint8_t Repeat = nms / 540;
    uint16_t Remain = nms % 540;

    while(Repeat)
    {
        SysTick_Delay_nms(540);
        Repeat -- ;
    }
    if(Remain) SysTick_Delay_nms(Remain);

}

/*---------------------------------END---------------------------------*/
